package com.ruoyi.knowledge.service.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ruoyi.knowledge.mapper.KnowledgeFileSegmentMapper;
import com.ruoyi.knowledge.domain.KnowledgeFileSegment;
import com.ruoyi.knowledge.service.IKnowledgeFileSegmentService;

/**
 * 知识库文件切片Service业务层处理
 * 
 * @author ruoyi
 */
@Service
public class KnowledgeFileSegmentServiceImpl implements IKnowledgeFileSegmentService 
{
    private static final Logger log = LoggerFactory.getLogger(KnowledgeFileSegmentServiceImpl.class);
    
    @Autowired
    private KnowledgeFileSegmentMapper knowledgeFileSegmentMapper;

    /**
     * 查询知识库文件切片
     * 
     * @param id 知识库文件切片主键
     * @return 知识库文件切片
     */
    @Override
    public KnowledgeFileSegment selectKnowledgeFileSegmentById(Long id)
    {
        return knowledgeFileSegmentMapper.selectKnowledgeFileSegmentById(id);
    }

    /**
     * 查询知识库文件切片列表
     * 
     * @param knowledgeFileSegment 知识库文件切片
     * @return 知识库文件切片
     */
    @Override
    public List<KnowledgeFileSegment> selectKnowledgeFileSegmentList(KnowledgeFileSegment knowledgeFileSegment)
    {
        return knowledgeFileSegmentMapper.selectKnowledgeFileSegmentList(knowledgeFileSegment);
    }

    /**
     * 根据文件ID查询切片列表
     * 
     * @param fileId 文件ID
     * @return 知识库文件切片集合
     */
    @Override
    public List<KnowledgeFileSegment> selectKnowledgeFileSegmentsByFileId(Long fileId)
    {
        return knowledgeFileSegmentMapper.selectKnowledgeFileSegmentsByFileId(fileId);
    }

    /**
     * 新增知识库文件切片
     * 
     * @param knowledgeFileSegment 知识库文件切片
     * @return 结果
     */
    @Override
    public int insertKnowledgeFileSegment(KnowledgeFileSegment knowledgeFileSegment)
    {
        Date now = new Date();
        knowledgeFileSegment.setCreatedAt(now);
        knowledgeFileSegment.setUpdatedAt(now);
        return knowledgeFileSegmentMapper.insertKnowledgeFileSegment(knowledgeFileSegment);
    }

    /**
     * 修改知识库文件切片
     * 
     * @param knowledgeFileSegment 知识库文件切片
     * @return 结果
     */
    @Override
    public int updateKnowledgeFileSegment(KnowledgeFileSegment knowledgeFileSegment)
    {
        knowledgeFileSegment.setUpdatedAt(new Date());
        return knowledgeFileSegmentMapper.updateKnowledgeFileSegment(knowledgeFileSegment);
    }

    /**
     * 批量删除知识库文件切片
     * 
     * @param ids 需要删除的知识库文件切片主键
     * @return 结果
     */
    @Override
    public int deleteKnowledgeFileSegmentByIds(Long[] ids)
    {
        return knowledgeFileSegmentMapper.deleteKnowledgeFileSegmentByIds(ids);
    }

    /**
     * 删除知识库文件切片信息
     * 
     * @param id 知识库文件切片主键
     * @return 结果
     */
    @Override
    public int deleteKnowledgeFileSegmentById(Long id)
    {
        return knowledgeFileSegmentMapper.deleteKnowledgeFileSegmentById(id);
    }
    
    /**
     * 根据文件ID删除切片数据
     * 
     * @param fileId 文件ID
     * @return 结果
     */
    @Override
    public int deleteKnowledgeFileSegmentsByFileId(Long fileId)
    {
        return knowledgeFileSegmentMapper.deleteKnowledgeFileSegmentsByFileId(fileId);
    }
    
    /**
     * 批量保存知识库文件切片
     * 
     * @param segmentList 知识库文件切片列表
     * @return 结果
     */
    @Override
    public int batchSaveKnowledgeFileSegments(List<KnowledgeFileSegment> segmentList)
    {
        if (segmentList == null || segmentList.isEmpty()) {
            log.warn("批量保存切片数据时列表为空");
            return 0;
        }
        
        log.info("批量保存切片数据, 数量: {}", segmentList.size());
        Date now = new Date();
        
        // 设置创建和更新时间
        for (KnowledgeFileSegment segment : segmentList) {
            segment.setCreatedAt(now);
            segment.setUpdatedAt(now);
        }
        
        try {
            return knowledgeFileSegmentMapper.batchInsertKnowledgeFileSegments(segmentList);
        } catch (Exception e) {
            log.error("批量保存切片数据失败", e);
            throw e;
        }
    }
} 